<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

jimport( 'joomla.application.component.model' );

/**
 * @package Joomla
 * @subpackage JobBoard
 */
class JAJobBoardModelJAJobBoard extends JModel
{
	/**
	 * Get greeting string (This is demo)
	 * @return The greeting string to be displayed on screen
	 */
	function getGreeting(){
		return JTEXT::_('JBGREETING');
	}
	
	function getStatisticsInfo()
	{
		$statistics = array();
		//New jobs
		$statistics['ja_jobs']['approved']['all'] = $this->countNewRecord('ja_jobs',' and status="Approved"');
		$statistics['ja_jobs']['nonapproved']['all'] = $this->countNewRecord('ja_jobs',' and status="Non-Approved"');
		$statistics['ja_jobs']['waiting']['all'] = $this->countNewRecord('ja_jobs',' and status="Waiting"');
		$statistics['ja_jobs']['approved']['today'] = $this->countNewRecord('ja_jobs', ' and DATEDIFF(created_date,now())=0 and status="Approved"');
		$statistics['ja_jobs']['approved']['week'] = $this->countNewRecord('ja_jobs', ' and EXTRACT(WEEK FROM created_date)=EXTRACT(WEEK FROM now()) and status="Approved"');
		$statistics['ja_jobs']['approved']['month'] = $this->countNewRecord('ja_jobs', ' and EXTRACT(MONTH FROM created_date)=EXTRACT(MONTH FROM now()) and status="Approved"');
		$statistics['ja_jobs']['nonapproved']['today'] = $this->countNewRecord('ja_jobs', ' and DATEDIFF(created_date,now())=0 and status="Non-Approved"');
		$statistics['ja_jobs']['nonapproved']['week'] = $this->countNewRecord('ja_jobs', ' and EXTRACT(WEEK FROM created_date)=EXTRACT(WEEK FROM now()) and status="Non-Approved"');
		$statistics['ja_jobs']['nonapproved']['month'] = $this->countNewRecord('ja_jobs', ' and EXTRACT(MONTH FROM created_date)=EXTRACT(MONTH FROM now()) and status="Non-Approved"');
		$statistics['ja_jobs']['waiting']['today'] = $this->countNewRecord('ja_jobs', ' and DATEDIFF(created_date,now())=0 and status="Waiting"');
		$statistics['ja_jobs']['waiting']['week'] = $this->countNewRecord('ja_jobs', ' and EXTRACT(WEEK FROM created_date)=EXTRACT(WEEK FROM now()) and status="Waiting"');
		$statistics['ja_jobs']['waiting']['month'] = $this->countNewRecord('ja_jobs', ' and EXTRACT(MONTH FROM created_date)=EXTRACT(MONTH FROM now()) and status="Waiting"');
		$statistics['ja_jobs']['expired']['today'] = $this->countNewRecord('ja_jobs', ' and DATEDIFF(created_date,now())=0 and status="Expired"');
		$statistics['ja_jobs']['expired']['week'] = $this->countNewRecord('ja_jobs', ' and EXTRACT(WEEK FROM created_date)=EXTRACT(WEEK FROM now()) and status="Expired"');
		$statistics['ja_jobs']['expired']['month'] = $this->countNewRecord('ja_jobs', ' and EXTRACT(MONTH FROM created_date)=EXTRACT(MONTH FROM now()) and status="Expired"');
		$statistics['ja_jobs']['expired']['all'] = $this->countNewRecord('ja_jobs',' and status="Expired"');
		
		//New employers
		$statistics['ja_employers']['approved']['all'] = $this->countNewRecord('ja_profiles',' and approved=1');
		$statistics['ja_employers']['approved']['today'] = $this->countNewRecord('ja_profiles',' and DATEDIFF(created_date,now())=0 and approved=1');
		$statistics['ja_employers']['approved']['week'] = $this->countNewRecord('ja_profiles', 'and EXTRACT(WEEK FROM created_date)=EXTRACT(WEEK FROM now()) and approved=1');
		$statistics['ja_employers']['approved']['month'] = $this->countNewRecord('ja_profiles', 'and EXTRACT(MONTH FROM created_date)=EXTRACT(MONTH FROM now()) and approved=1');
		$statistics['ja_employers']['unapproved']['all'] = $this->countNewRecord('ja_profiles',' and approved=0');
		$statistics['ja_employers']['unapproved']['today'] = $this->countNewRecord('ja_profiles',' and DATEDIFF(created_date,now())=0 and approved=0');
		$statistics['ja_employers']['unapproved']['week'] = $this->countNewRecord('ja_profiles', 'and EXTRACT(WEEK FROM created_date)=EXTRACT(WEEK FROM now()) and approved=0');
		$statistics['ja_employers']['unapproved']['month'] = $this->countNewRecord('ja_profiles', 'and EXTRACT(MONTH FROM created_date)=EXTRACT(MONTH FROM now()) and approved=0');
		
		//New resumes
		$statistics['ja_resumes']['approved']['all'] = $this->countNewRecord('ja_resumes',' and approved=1');
		$statistics['ja_resumes']['approved']['today'] = $this->countNewRecord('ja_resumes',' and DATEDIFF(created_date,now())=0 and approved=1');
		$statistics['ja_resumes']['approved']['week'] = $this->countNewRecord('ja_resumes', 'and EXTRACT(WEEK FROM created_date)=EXTRACT(WEEK FROM now()) and approved=1');
		$statistics['ja_resumes']['approved']['month'] = $this->countNewRecord('ja_resumes', 'and EXTRACT(MONTH FROM created_date)=EXTRACT(MONTH FROM now()) and approved=1');
		$statistics['ja_resumes']['unapproved']['all'] = $this->countNewRecord('ja_resumes',' and approved=0');
		$statistics['ja_resumes']['unapproved']['today'] = $this->countNewRecord('ja_resumes',' and DATEDIFF(created_date,now())=0 and approved=0');
		$statistics['ja_resumes']['unapproved']['week'] = $this->countNewRecord('ja_resumes', 'and EXTRACT(WEEK FROM created_date)=EXTRACT(WEEK FROM now()) and approved=0');
		$statistics['ja_resumes']['unapproved']['month'] = $this->countNewRecord('ja_resumes', 'and EXTRACT(MONTH FROM created_date)=EXTRACT(MONTH FROM now()) and approved=0');
		
		//New applications
//		$statistics['ja_applications']['all'] = $this->countNewRecord_Today('ja_applications');
		
		//New transactions
		$statistics['ja_transactions']['all'] = $this->sumNewRecord('ja_transactions');
		$statistics['ja_transactions']['today'] = $this->sumNewRecord('ja_transactions',' and DATEDIFF(created_date,now())=0');
		$statistics['ja_transactions']['week'] = $this->sumNewRecord('ja_transactions',' and EXTRACT(WEEK FROM created_date)=EXTRACT(WEEK FROM now())');
		$statistics['ja_transactions']['month'] = $this->sumNewRecord('ja_transactions',' and EXTRACT(MONTH FROM created_date)=EXTRACT(MONTH FROM now())');
		return $statistics;
	}
	function countNewRecord($table_name,$where_more='')
	{
		$db = JFactory::getDBO();
		$query = "SELECT count(id) AS qty FROM #__$table_name WHERE 1=1 ";
		
		if ($where_more!='')
			$query .= $where_more;
		$db->setQuery($query);
		$qty = $db->loadObject();
		return $qty->qty;
	}
	
		function sumNewRecord($table_name,$where_more='')
	{
		$db = JFactory::getDBO();
		$query = "SELECT sum(amount) AS qty FROM #__$table_name WHERE completed=1 ";
		
		if ($where_more!='')
			$query .= $where_more;
			$db->setQuery($query);
		$qty = $db->loadObject();
		return $qty->qty;
	}
	
	function getSearchOption()
	{
		$arr_option = array();
		$arr_option[0]->value = '';
		$arr_option[0]->text = '-------'.JText::_('Select search type').'-------';
		$arr_option[1]->value = 'jajobs';
		$arr_option[1]->text = JText::_('Jobs');
		$arr_option[2]->value = 'jaemployers';
		$arr_option[2]->text = JText::_('Employers');
		$arr_option[3]->value = 'jacandidates';
		$arr_option[3]->text = JText::_('Job Seekers');
		$arr_option[4]->value = 'jatransactions';
		$arr_option[4]->text = JText::_('Transactions');
		$obj_Search_Type = JHTML::_('select.genericlist',$arr_option,'view','class="inputbox" size="1"','value','text','');
		return $obj_Search_Type;
	}

	function getKey(){
		global $jbconfig;		
		$sql = "select data from #__ja_config where tab='key'";
		$db = JFactory::getDBO();
		$db->setQuery($sql);
		$key = $db->loadResult();
		return $key;
	}
		
	function getLatestVersion(){
		global $JBVERSION;
		global $JBPRODUCTKEY;
		if(isset($_SESSION['latest_version'])) 
			$latest_version =  $_SESSION['latest_version'];
		else 
		{			
			$req = 'key=com_jajobboard';
			$req .= '&type=product_name';
			$host = 'www.joomlart.com';
		    $path = '/forums/getlatestversion.php';
		    $URL = "http://$host$path";
		    $latest_version = '';
		    if(!function_exists('curl_version')) {
		    	if (stristr(ini_get('disable_functions'), "fsockopen")) {
		            return ;
		        }
		        else{	      
		        	$latest_version = @socket_getdata($host, $path, $req);
		        }
		    }
		    else{ 
			   $latest_version = @curl_getdata($URL, $req);	   
		   }
		   if( strlen($latest_version) > 1000 ){
				$latest_version = "";
			}
		}		
		$_SESSION['latest_version'] = $latest_version;		
		
	   return $latest_version;
	}	
}
?>
